﻿var FileSelect = angular.module("FileSelect", ['restangular','Common','loadModule']);



FileSelect.directive('selectfilemodal',['RequestTrainService', "sysUserInfo","Msg",function(RS,sysUserInfo,Msg){
  return {
    scope:{
        ApiData:'=jisuModel',
        onSay: '&'
    },
    link: function($scope, $element, $attrs) {
    
    $scope.ApiData.kdids="0";
    $scope.ApiData.fpath= "/";

    $scope.ApiData.isbtnload = false;
    $scope.ApiData.loading = false;
    $scope.say = function(){$scope.onSay();}//复制、移动文件
    
    //查询筛选条件
    $scope.params = { "userid":  sysUserInfo.getuserinfo().user_ID, "fid": "", "orgid":  sysUserInfo.getuserinfo().organization_ID, "searchText": "", "pageIndex": "1", "pageSize": "100","searchType":"3","tagid":"","fileType":"folder","orderby":"desc" ,"powerLV": sysUserInfo.getuserinfo().powerLV };
    
    //文件排序筛选
    $scope.ddmovefile = { "divclass": "knowledgeright-header-right_paixu", "showflag": false, "state": "1", objinfo: [], optype: "1", "divulclass": "knowledgeright-header-right_paixu dropdown-menu", "ddicon": "&#xe6b1;","orderby":""};

    $scope.ddmovefile.objinfo = [{ "ddname": "文件名称", "ddvalue": "1", "ddicon": "" }, { "ddname": "文件创建时间", "ddvalue": "3", "ddicon": "" }, { "ddname": "文件修改时间", "ddvalue": "4", "ddicon": ""}];
    
    //查看全部文件
    $scope.selectAllkn = function () {
        $scope.params.tagid = "";
        $scope.kblist = [];
        $scope.crumbsknlist = [];
        $scope.params.fid = "0";
        $scope.params.pageIndex = 1;
        $scope.params.searchText = "";
        $scope.GetkbList();
        //当前选中的对象
        //        $scope.selkninfo = "";
        //面包屑目录信息
        $scope.crumbsknlist = [];

        $scope.kbinfo.fileName ="";
        $scope.kbinfo.filepath ="/"; 
        $scope.kbinfo.fid ="0"; 
        $scope.kbinfo.upId ="0"; 
        $scope.ApiData.kdids="0";
    }

    //页面监听排序
    $scope.$watch('ddmovefile.state', function (newValue, oldValue, scope) {        
//        console.log($scope.ddmovefile.state);
        $scope.params.searchType=$scope.ddmovefile.state; 
        $scope.params.tagid="";
        $scope.params.pageIndex=1;  
        $scope.GetkbList();
    }, true);


        //页面监听排序
    $scope.$watch('ApiData.knshow', function (newValue, oldValue, scope) {        
        if($scope.ApiData.knshow){            
            $scope.params.tagid="";
            $scope.params.pageIndex=1;  
            $scope.GetkbList();
        }
    }, true);

     //页面监听排序
    $scope.$watch('ddmovefile.orderby', function (newValue, oldValue, scope) {      
        $scope.params.searchType=$scope.ddmovefile.state;       
        $scope.params.orderby=$scope.ddmovefile.orderby;
        $scope.params.pageIndex=1;  
        $scope.GetkbList();    
    }, true);
     //搜索监听
    $scope.searchkeyup = function (e) {
        var keycode = window.event ? e.keyCode : e.which;
        if (keycode == 13) {
            console.log('执行了回车');
            $scope.searchBtn();
        }
    }
    //当前选中的对象
    $scope.selkninfo="";
    //面包屑目录信息
    $scope.crumbsknlist=[];
    //文件属性标签
    $scope.extend = { "oldtag": RS.getOldTagList(), "newtag": [{ "index": "0", "tagid": "0", "tagname": "全部标签", "tagcolor": "#icon-biaoqiantoumingquanbu" }] };
     
     $scope.kblist=[];
     
    //页面监听标签筛选
    $scope.$watch('extend.newtag', function (newValue, oldValue, scope) {
//            if($scope.kblist.length==0){
//                
//            }else{
    if($scope.ApiData.optype==""){
        $scope.params.tagid=""; 
    }else{
        $scope.params.tagid=$scope.extend.newtag[0].tagid; 
    }
//            }
//            $scope.params.tagid=newValue.tagid;  
            $scope.params.pageIndex=1;  
            $scope.crumbsknlist = [];
            $scope.GetkbList();
    }, true);


    //关闭
    $scope.close=function(){
        $scope.ApiData.knshow=false;
        $scope.ApiData.isbtnload = false;
        $scope.params.tagid = "";
        $scope.kblist = [];
        $scope.crumbsknlist = [];
        $scope.params.fid = "0";
        $scope.params.pageIndex = 1;
        $scope.params.searchText = "";   
        $scope.GetkbList();     
        //面包屑目录信息
        $scope.crumbsknlist = [];
        $scope.kbinfo.fileName ="";
        $scope.kbinfo.filepath ="/"; 
        $scope.kbinfo.fid ="0"; 
        $scope.kbinfo.upId ="0"; 

    };

        //点击标签查询对应文件夹/文件
    $scope.tagsel = function (taginfo) {
        console.log(taginfo);
        $scope.params.tagid = taginfo.tagid;
        $scope.kblist = [];
        $scope.kbids = [];
        $scope.params.pageIndex = 1;
        $scope.knList();
    }

      //获取文件对应的标签
    $scope.getFileTagList = function (tagids) {
        var taglist = [];
        if (tagids != undefined && tagids != "") {
            angular.forEach($scope.extend.oldtag, function (taginfo, index) {
                if (tagids.indexOf($scope.extend.oldtag[index].tagid) >= 0) {
                    if (taglist.length >= 3) {
                        //break;   前台只显示3个标签图标
                    }else{
                        taglist.push($scope.extend.oldtag[index]);
                    }
                }
            });
        }
        return taglist;
    }
    //获取文件对应的标签（相应的样式）
    $scope.getFileTagClass = function (tagids) {

        var classname = "knowledgebase_select_move_thumbnailtext";
        var taglist = [];
        if (tagids != undefined && tagids != "") {
            angular.forEach($scope.extend.oldtag, function (taginfo, index) {
                if (tagids.indexOf($scope.extend.oldtag[index].tagid) >= 0) {
                    if (taglist.length >= 3) {
                        //break;   前台只显示3个标签图标
                    } else {
                        taglist.push($scope.extend.oldtag[index]);
                    }
                }
            });
        }

        if (taglist.length == 1) { classname = "knowledgebase_select_move_thumbnailtext biaoqianfirst"; }
        else if (taglist.length == 2) { classname = "knowledgebase_select_move_thumbnailtext biaoqiansecond"; }
        else if (taglist.length == 3) { classname = "knowledgebase_select_move_thumbnailtext biaoqianthird"; }
        return classname;
    }

    //搜索
    $scope.searchBtn=function(){
       $scope.params.tagid="";
        $scope.params.pageIndex=1;  
        $scope.GetkbList();
    }


    //请求数据
    $scope.GetkbList=function(){
        $scope.ApiData.loading = true;
        RS.getuserfile($scope.params).then(function (kbdata) {
            $scope.ApiData.loading = false;
            if (kbdata.errorcode == "0") {
                $scope.kblist = kbdata.datas;
            }else{
                Msg.error("请求数据有误！");
            }
        }, function callback() {
            Msg.error("请求数据异常！");
        });
    }

    $scope.alltag=function(){

    };
    /*
    //点击选中
    $scope.selkb=function(kdid){    
        console.log("点击：");
        console.log(kdid);
         $scope.ApiData.kdids=kdid.upId;
         $scope.ApiData.fpath=kdid.filepath;

         angular.forEach($scope.kblist, function (i, id) {
            //遍历判断checkbox是否已选
            if ($scope.kblist[id].upId == kdid.upId) {                
                $scope.kblist[id].sel = true;
            }else{
                $scope.kblist[id].sel = false;
            }
        });        
    }
    */
    Array.prototype.remove=function(dx){
        if(isNaN(dx)||dx>this.length){return false;}
        for(var i=0,n=0;i<this.length;i++){
            if(this[i]!=this[dx]){
                this[n++]=this[i]
            }
        }
        this.length-=1
　}

Array.prototype.baoremove = function(dx){ 
    if(isNaN(dx)||dx>this.length){return false;}
    this.splice(dx,1);
}


    //显示文件夹
    $scope.showupnamefun = function (title, name, flag) {
        $scope.upsharedname = name; // 修改创建文件夹名字
        $scope.upsharedtitle = title;   // 弹框标题
        $scope.upsharedflag = flag;     // 判断什么类型的弹框
        $scope.showupname = true;
    }

// 创建文件夹
    $scope.createfolder = function () {
        $scope.showupname = true;
        $scope.showupnamefun('新建文件夹', '', 0);

        if ($scope.crumbsknlist.length == 0) {
            $scope.kbinfo = { "upUserId": sysUserInfo.getuserinfo().user_ID, "upOrgId": sysUserInfo.getuserinfo().organization_ID, "userName": sysUserInfo.getuserinfo().username, "fileName": "", "filepath": "/", "fid": "0", "upId": "0" };
        } else {
            $scope.kbinfo = { "upUserId": sysUserInfo.getuserinfo().user_ID, "upOrgId": sysUserInfo.getuserinfo().organization_ID, "userName": sysUserInfo.getuserinfo().username, "fileName": "", "filepath": $scope.crumbsknlist[0].filepath + $scope.crumbsknlist[0].fileName, "fid": $scope.crumbsknlist[0].upId, "upId": "0" };
        }
    }

    $scope.showupname = false;
    $scope.upsharedflag = 0;    //0  新增文件夹  1修改文件名称
    $scope.closeupname = function () {
        $scope.showupname = false;
    }
    $scope.kbinfo = { "upUserId": sysUserInfo.getuserinfo().user_ID, "upOrgId": sysUserInfo.getuserinfo().organization_ID, "userName": sysUserInfo.getuserinfo().username, "fileName": "", "filepath": "/", "fid": "0", "upId": "0" };
      //编辑/保存文件名称
    $scope.saveupname = function () {    
        $scope.ApiData.loading = true;
         if ($scope.upsharedflag == 0) {  // 创建文件夹
            RS.CreateFolder($scope.kbinfo).then(function (kbdata) {
                $scope.ApiData.loading = false;
                if (kbdata.errorcode == 0) {
                    Msg.success("保存成功！");
                    $scope.showupname = false;
                    $scope.kblist.push(kbdata.data);
                } else if (kbdata.errorcode == 29) {
                    Msg.info("文件或文件夹名称已经存在");
                } else {
                    Msg.error("操作失败！");
                }
            });
        }
    }
 //移除当前正在操作的文件夹
$scope.repeatlist=function(kbinfo){
    var showselectinfo=false;
         for( var j=0;j<$scope.ApiData.commenId.length;j++){
                if(kbinfo.upId==$scope.ApiData.commenId[j]){
                    showselectinfo=true;
                }
         }
         return showselectinfo;
}
//返回上一级
$scope.backlevel=function(){
    $scope.params.tagid="";
    $scope.params.fid=$scope.selkninfo.fid;
    $scope.GetkbList();
    $scope.ApiData.kdids="";
}
    //双击进入下一级
    $scope.dbselkb=function(kdinfo){
        if($scope.repeatlist(kdinfo)){
            return;
        }
     $scope.params.tagid="";
        console.log("双击："+kdinfo.upId);
        $scope.selkninfo=kdinfo;//记录点击对象

        if($scope.crumbsknlist.length==0){
            $scope.crumbsknlist.push(kdinfo);
        }else{
            var flag = false;var del_index=0;
            for(var i=0;i<$scope.crumbsknlist.length;i++){
                if($scope.crumbsknlist[i].upId==kdinfo.upId){
                    del_index=i;
                    flag=true;
                }
            }
            if(flag){
                for(var i=$scope.crumbsknlist.length-1;i>del_index;i--){
                    $scope.crumbsknlist.baoremove(i);
                }
                //$scope.crumbsknlist=$scope.cknlist.splice(1,$scope.cknlist.length-del_index) ;
            }else{
                $scope.crumbsknlist.push(kdinfo);
            }
        }

        console.log($scope.crumbsknlist);

        //筛选下一级目录
        $scope.params.fid=kdinfo.upId;
        $scope.GetkbList();
        //$scope.ApiData.kdids="";

        $scope.ApiData.kdids=kdinfo.upId;
        $scope.ApiData.fpath=kdinfo.filepath;

    }

    //$scope.GetkbList();
    },
    replace:true,
    templateUrl:"/system/directive/train/movefile.html",
        restrict: 'EC', //E = Element, A = Attribute, C = Class, M = Comment
    }
}]);
